package com.lakala.sh.bps.bpsmerchant.client.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lakala.sh.bps.bpsmerchant.client.enmu.Command;
import com.lakala.sh.bps.bpsmerchant.client.model.request.CommandRequestVO;
import com.lakala.sh.bps.bpsmerchant.client.model.request.MchContractBankSelRequest;
import com.lakala.sh.bps.bpsmerchant.client.model.response.BankInfoResponseVO;
import com.lakala.sh.bps.bpsmerchant.client.model.response.BaseResponseVO;
import com.lakala.sh.bps.bpsmerchant.client.utils.HttpClientUtil;
import com.lakala.sh.bps.bpsmerchant.client.utils.SignUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;


/**
 * @author: peijian
 * @date: 2019/12/2 15:35
 */
public class MainEntry {

    /**
     * 测试环境地址
     */
    private static final String BPS_TEST_URL = "https://test.wsmsd.cn/sit/bps/api/mch/cmd";

    private static final String BPS_PRD_URL = "https://s3.lakala.com/bps/api/mch/cmd";
    /**
     * 接入时申请的APPID
     */
    private static final String appid = "324814";
    /**
     * 接入时申请的TOKEN
     */
    private static final String token = "cc0cfdacbf047bff036f1fe691bc91a1";
    /**
     * 接入时申请的secret_key
     */
    private static final String secretKey = "61aae896102be2859b8843895093fb72";


    public static void main(String[] args) {
        MainEntry mainEntry = new MainEntry();
        //获取银行卡信息
        mainEntry.getBankInfos();
        //获取进件信息
        mainEntry.mchApply();
    }

    private void mchApply() {
        
    }

    /**
     * 获取银行卡信息demo
     */
    private void getBankInfos() {
        try {
            MchContractBankSelRequest bankSelRequest = new MchContractBankSelRequest();
            bankSelRequest.setBankName("田园新城支行");
            bankSelRequest.setOpeningBank("105491001855");
            bankSelRequest.setDistrictCode("4910");
            CommandRequestVO<MchContractBankSelRequest> vo = new CommandRequestVO();
            vo.setAppid(appid);
            vo.setToken(token);
            vo.setCmd(Command.MCH_CONTRACT_BANK_SEL);
            vo.setReqData(bankSelRequest);
            String signStr = SignUtil.md5Sign(JSON.toJSONString(bankSelRequest), secretKey);
            System.out.println("签名sign="+signStr);
            vo.setSign(signStr);
            String result = HttpClientUtil.doPostByJson(BPS_PRD_URL, JSON.toJSONString(vo));
            System.out.println("响应报文="+result);
            if(StringUtils.isNotEmpty(result)) {
                BaseResponseVO baseResponseVO = JSON.parseObject(result, BaseResponseVO.class);
                if("BPS00000".equals(baseResponseVO.getRetCode())) {
                    String jsonString = JSON.toJSONString(baseResponseVO.getRespData());
                    JSONObject jsonObject = (JSONObject) JSON.parse(jsonString);
                    if(jsonObject.get("banks") != null) {
                        JSONArray wechatFeesArray = (JSONArray) jsonObject.get("banks");
                        List<BankInfoResponseVO> banks = JSON.parseArray(JSON.toJSONString(wechatFeesArray), BankInfoResponseVO.class);
                        /**
                         * TODO 业务逻辑处理
                         */
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
